package com.desaysv.wandroid.ui.web

import android.annotation.SuppressLint
import android.content.Intent
import android.graphics.Bitmap
import android.net.http.SslError
import android.os.Build
import android.text.TextUtils
import android.util.Log
import android.view.View
import android.webkit.*
import com.desaysv.wandroid.base.BaseActivity
import com.desaysv.wandroid.databinding.ActivityAirticWebBinding
import com.desaysv.wandroid.databinding.ActivityBaseBinding
import com.desaysv.wandroid.ui.main.MainActivity
import com.desaysv.wandroid.ui.web.mvp.AirticWebContract
import com.desaysv.wandroid.ui.web.mvp.AirticWebModel
import com.desaysv.wandroid.ui.web.mvp.AirticWebPersenter

class AirticWebActivity : BaseActivity<AirticWebContract.IAirticView,AirticWebModel,AirticWebPersenter>(),AirticWebContract.IAirticView {
    private lateinit var mWebBinding:ActivityAirticWebBinding
    private val TAG = "AirticWebActivity"
    companion object{
        val PARAMS_URL="URL"
        val PARAMS_TITLE="TITLE"
    }
    private var url=""
    override fun loadWeb(url: String) {
        mWebBinding.web.loadUrl(url)
    }

    override fun setPresenter(): AirticWebPersenter? {
        return AirticWebPersenter()
    }

    override fun setView(): View {
        mWebBinding=ActivityAirticWebBinding.inflate(layoutInflater)
        return mWebBinding.root
    }
    @SuppressLint("SetJavaScriptEnabled")
    override fun init(base: ActivityBaseBinding) {
        url= intent.getStringExtra(PARAMS_URL).toString()
        title= intent.getStringExtra(PARAMS_TITLE).toString()

        noAdd()

        Log.e(TAG, "init: "+url )

        mWebBinding.progress.visibility=View.GONE
        var chromeClient=object :WebChromeClient(){
            override fun onProgressChanged(view: WebView?, newProgress: Int) {
                super.onProgressChanged(view, newProgress)
                mWebBinding.progress.progress=newProgress
            }
        }
        var webviewClient=object :WebViewClient(){
            override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {

                if(url!!.startsWith("http")){
                    mWebBinding.web.loadUrl(url!!)
                }
                return true
            }

            override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) {
                super.onPageStarted(view, url, favicon)
                mWebBinding.progress.visibility=View.VISIBLE
            }
            override fun onPageFinished(view: WebView?, url: String?) {
                super.onPageFinished(view, url)
                mWebBinding.progress.visibility=View.GONE
            }

            @SuppressLint("WebViewClientOnReceivedSslError")
            override fun onReceivedSslError(
                view: WebView?,
                handler: SslErrorHandler?,
                error: SslError?
            ) {
                handler?.proceed()
            }

        }
        mWebBinding.web.webChromeClient=chromeClient
        mWebBinding.web.webViewClient=webviewClient
        val webSettings=mWebBinding.web.settings

        webSettings.allowFileAccessFromFileURLs=true
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {
            webSettings.mixedContentMode = WebSettings.MIXED_CONTENT_ALWAYS_ALLOW
        }
        //如果访问的页面中要与Javascript交互，则webview必须设置支持Javascript
        webSettings.setJavaScriptEnabled(true)

        //设置自适应屏幕，两者合用
        webSettings.setUseWideViewPort(true) //将图片调整到适合webview的大小
        webSettings.setLoadWithOverviewMode(true) // 缩放至屏幕的大小
        //自适应屏幕
        webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN)
        webSettings.setLoadWithOverviewMode(true) // 缩放至屏幕的大小

        //缩放操作
        webSettings.setSupportZoom(true) //支持缩放，默认为true。是下面那个的前提。
        webSettings.setBuiltInZoomControls(true) //设置内置的缩放控件。若为false，则该WebView不可缩放
        webSettings.setDisplayZoomControls(false) //隐藏原生的缩放控件

        //其他细节操作
        webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK) //关闭webview中缓存
        webSettings.setAllowFileAccess(true) //设置可以访问文件
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true) //支持通过JS打开新窗口
        webSettings.setLoadsImagesAutomatically(true) //支持自动加载图片
        webSettings.setDefaultTextEncodingName("utf-8")//设置编码格式

        //优先使用缓存
        webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK)


//        loadWeb("http://www.baidu.com")
//        loadWeb("http://linziblog.cn")
        mWebBinding.root.post {
            loadWeb(url)
        }
    }

    override fun onResume() {
        super.onResume()
    }

    override fun back() {
        var intent=Intent()
        setResult(RESULT_OK, intent)
        finish()
    }

    override fun add() {
    }
}